package cn.itsource.pethome.system.dept.controller;

import cn.itsource.pethome.annotation.PreAuthorize;
import cn.itsource.pethome.system.dept.domain.Department;
import cn.itsource.pethome.system.dept.query.DepartmentQuery;
import cn.itsource.pethome.system.dept.service.IDepartmentService;
import cn.itsource.pethome.utils.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author:R
 * @Date:2022/12/31 11:06
 * Description
 */
@RestController
@RequestMapping("/dept")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartController {
    @Autowired
    private IDepartmentService departmentService;

    @ApiOperation(value = "查询全部部门信息" )//swagger的注释
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @PreAuthorize(name = "查询全部部门信息",value = "dept:post")
    public List<Department> findAll(){
        return departmentService.findAll();
    }

    @ApiOperation(value = "根据id查询一个部门" )//swagger的注释
    @GetMapping("/{id}")
    @PreAuthorize(name = "查询一个部门",value = "dept:findOne")
    public AjaxResult findOne(@PathVariable("id") Long id){
        try {
            Department one = departmentService.findOne(id);
            //将查询结果封装返回给前端
            return AjaxResult.success().setData(one);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

    @ApiOperation(value = "分页查询" )//swagger的注释
    @PostMapping("/pageList")
    @PreAuthorize(name = "部门分页查询",value = "dept:pageList")
    public AjaxResult findQuery(@RequestBody DepartmentQuery query){
        try {
            List<Department> query1 = departmentService.findQuery(query);
            Long aLong = departmentService.countPage(query);
            return AjaxResult.success().setData(query1).setTotal(aLong);
        }catch (Exception e){
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

    @ApiOperation(value = "根据id删除部门" )//swagger的注释
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "删除部门",value = "dept:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return AjaxResult.success().setMsg("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }

    }

    @ApiOperation(value = "保存部门 【有id为修改】" )//swagger的注释
    @PutMapping
    @PreAuthorize(name = "保存部门信息",value = "dept:save")
    public AjaxResult save(@RequestBody Department department){
        try {
            if (department.getId() != null){
                departmentService.update(department);
                return AjaxResult.success().setMsg("修改成功!");
            }else {
                departmentService.add(department);
                return AjaxResult.success().setMsg("添加成功！");
            }
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }

    @ApiOperation(value = "根据id批量删除部门" )//swagger的注释
    @PatchMapping
    @PreAuthorize(name = "批量删除部门信息",value = "dept:batchDel")
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        try {
            departmentService.batchDel(ids);
            return AjaxResult.success().setMsg("批量删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error().setMsg("批量删除失败");
    }

    @RequestMapping(value = "/tree",method = RequestMethod.GET)
    public List<Department> deptTree(){
        return departmentService.deptTree();
    }
}
